package routers

import (
	"context"
	"gcp-api-proxy/internal/conf"
	"gcp-api-proxy/pkg/log"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"go.uber.org/zap"
	"io"
	"net/http"
	// dao "gsc-datasets/internal/db/dao"
	// pgdb "gsc-datasets/internal/db/sqlc"
	// upcos "gsc-datasets/pkg/upstream/scf/cos"
	// upsms "gsc-datasets/pkg/upstream/scf/sms"
	// "gsc-datasets/internal/rest/service"
)

type RestRouter struct {
	proxy RestRouterProxy
	r     chi.Router
}

type RestRouterProxy interface {
	log.LoggerProxy
	conf.GCPProxy
}

// New initialization
func New(
	proxy RestRouterProxy,
) *RestRouter {
	return &RestRouter{
		proxy: proxy,
		r:     chi.NewRouter(),
	}
}

// func (s *RestRouter) NewService() *service.Service {
// 	return service.New(s.slog, s.dao)
// }

func (s *RestRouter) ChiRouter() chi.Router {
	return s.r
}

func (s *RestRouter) Logger(module string) *zap.SugaredLogger {
	return s.proxy.GetLogger().With("module", module)
}

func (s *RestRouter) GetReqID(ctx context.Context) string {
	return middleware.GetReqID(ctx)
}

func (s *RestRouter) PrintBody(r *http.Request) {
	bodyBytes, err := io.ReadAll(r.Body)
	// Replace the body with a new reader after reading from the original
	if err != nil {
		return
	}
	s.proxy.GetLogger().Infof("req body=>%s", string(bodyBytes))
}

func (s *RestRouter) GetProxy() RestRouterProxy {
	return s.proxy
}

// impl interface shared.LoggerProxy
func (s *RestRouter) GetLogger() *zap.SugaredLogger {
	return s.proxy.GetLogger()
}
